#include "aliyun_client.h"
#include <Arduino.h>
#include <some_config.h>
#include "PubSubClient.h" ////A client library for MQTT messaging.
#include <WiFiClient.h>
#include <WiFi.h>
#include <string.h>
#include <NTPClient.h>

WiFiClient espClient;
PubSubClient client(espClient);

#define NTP_SERVER "ntp.aliyun.com"
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_SERVER);



void print_some()
{
    Serial.printf("\n                   _ooOoo_\r\n");
    Serial.printf("                  o8888888o\r\n");
    Serial.printf("                  88\" . \"88\r\n");
    Serial.printf("                  (| -_- |)\r\n");
    Serial.printf("                  O\\  =  /O\r\n");
    Serial.printf("               ____/`---'\\____\r\n");
    Serial.printf("             .'  \\\\|  %%  |//  `.\r\n");
    Serial.printf("            /  \\\\|||  %%  |||//  \\\r\n");
    Serial.printf("           /  _||||| -%%- |||||-  \\\r\n");
    Serial.printf("           |   | \\\\\\  -  /// |   |\r\n");
    Serial.printf("           | \\_|  ''\\---/''  |   |\r\n");
    Serial.printf("           \\  .-\\__  `-`  ___/-. /\r\n");
    Serial.printf("         ___`. .'  /--.--\\  `. . __\r\n");
    Serial.printf("      .\"\" '<  `.___\\_<|>_/___.\'  >\"\".\r\n");
    Serial.printf("     | | :  `-. \\`.;\\ _ /`;.`/ - ` : | |\r\n");
    Serial.printf("     \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /\r\n");
    Serial.printf("======`-.____`-.___\\_____/___.-`____.-'======\r\n");
    Serial.printf("                   `=---='\n");
    Serial.printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n");
    Serial.printf("  佛祖保佑         永无BUG         永不宕机\r\n");
}


void mqtt_init()
{
    client.setServer(MQTT_SERVER, MQTT_PORT); /* 连接MQTT服务器 */
    timeClient.setTimeOffset(8 * 3600);
    timeClient.begin();
    timeClient.update();
    Serial.println("Try to be a rainbow in someone's cloud.");
    Serial.printf("%s miss you\n", timeClient.getFormattedTime().c_str());
}

int mqttCheckConnect()
{
    int err_count = 0;
    timeClient.update();
    while (!client.connected())
    {
        Serial.println("Connecting to MQTT Server ...");
        if (client.connect(CLIENT_ID, MQTT_USRNAME, MQTT_PASSWD))
        {
            Serial.printf("MQTT Connected at %s!\n", timeClient.getFormattedTime());
        } else
        {
            Serial.print("MQTT Connect err:");
            Serial.println(client.state());
            err_count++;
            if (err_count > 3)
            {
                return -1;
            }
            bsp_delay(5000);
        }
    }
    return 0;
}

void aliyun_mqtt_send_data(char *target, float data)
{
    char param[32];
    char jsonBuf[128];
    if (mqttCheckConnect() == -1)
    {
        Serial1.println("mqtt connect error");
    }

    sprintf(param, "{\"%s\":%.2f}", target, data);
    sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
    Serial.print(jsonBuf);
    boolean b = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
    if (b)
    {
        Serial.println("publish success");
    } else
    {
        Serial.println("publish fail");
    }
}

void aliyun_mqtt_send_data(char *target, int data)
{
    char param[32];
    char jsonBuf[128];
    if (mqttCheckConnect() == -1)
    {
        Serial1.println("mqtt connect error");
    }
    sprintf(param, "{\"%s\":%d}", target, data);
    sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
    Serial.print(jsonBuf);
    boolean b = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
    if (b)
    {
        Serial.println("publish success");
    } else
    {
        Serial.println("publish fail");
    }
}

void aliyun_mqtt_send_data(char *target, char *data)
{
    char param[32];
    char jsonBuf[128];
    if (mqttCheckConnect() == -1)
    {
        Serial1.println("mqtt connect error");
    }
    sprintf(param, "{\"%s\":\"%s\"}", target, data);
    sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
    Serial.print(jsonBuf);
    boolean b = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
    if (b)
    {
        Serial.println("publish success");
    } else
    {
        Serial.println("publish fail");
    }
}
